package com.charonmc.commands;

import com.charonmc.CharonLogin;
import com.charonmc.utils.mysql.SqlManager;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.scheduler.BukkitRunnable;

public class AutoLogin implements CommandExecutor {
    @Override
    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
        if (sender instanceof Player){
            Player player = (Player) sender;
            if (!CharonLogin.loginPlayers.contains(player)){
                return true;
            }
            BukkitRunnable task = new BukkitRunnable() {
                @Override
                public void run() {
                    SqlManager SqlManager;
                    try {
                        SqlManager = new SqlManager("charonmc");
                        Object playerId = SqlManager.createQuery()
                                .inTable("players")
                                .selectColumns("player_id")
                                .addCondition("player_name", player.getPlayer().getName())
                                .build()
                                .execute()
                                .first();
                        Object autologin = SqlManager.createQuery()
                                .inTable("player_set")
                                .selectColumns("auto_login")
                                .addCondition("player_id", playerId)
                                .build()
                                .execute()
                                .first();
                        if ((int) autologin == 0){
                            SqlManager.createUpdate()
                                    .inTable("player_set")
                                    .addColumnNames(new String[]{"auto_login"})
                                    .addParams(new Object[]{1})
                                    .addCondition("player_id", playerId)
                                    .build()
                                    .execute();
                            sender.sendMessage("§7自动登录已 §a§l开启§7！");
                            sender.sendMessage("§7它的有效期为 §a§l20§7分钟！");
                            CharonLogin.autoLoginPlayer.put(player, 24000);
                        } else {
                            SqlManager.createUpdate()
                                    .inTable("player_set")
                                    .addColumnNames(new String[]{"auto_login"})
                                    .addParams(new Object[]{0})
                                    .addCondition("player_id", playerId)
                                    .build()
                                    .execute();
                            sender.sendMessage("§7自动登录已 §c§l关闭§7！");
                        }
                        SqlManager.close();
                    } catch (Exception e) {
                        throw new RuntimeException(e);
                    }
                }
            };
            task.runTaskAsynchronously(CharonLogin.instance);
        }
        return true;
    }
}
